/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import systole.domain.clinicalInformation.Pathology;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.PathologyBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class PathologyBrokerDB extends BrokerDB implements PathologyBroker {

    /**
     */
    public PathologyBrokerDB() {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.PathologyBroker#getAllPathologies()
     */
    @Override
    public List<Pathology> getAllPathologies() throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all patholigies");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Pathology> list = currentSession.createQuery("from Pathology p order by upper(p.name)").list();
            this.logger.logDebug("get patholigies successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get patholigies, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las patologías", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.PathologyBroker#insert(systole.domain.clinicalInformation.Pathology)
     */
    @Override
    public void insert(Pathology pathology) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving pathology");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(pathology);
            this.logger.logDebug("save pathology successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save pathology, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.PathologyBroker#update(systole.domain.clinicalInformation.Pathology)
     */
    @Override
    public void update(Pathology pathology) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating pathology");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(pathology);
            this.logger.logDebug("update pathology successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update pathology, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existPathologyByName(String name, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist Pathology by name");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Pathology.class).
                    add((id == null) ? Restrictions.eq("name", name)
                    : Restrictions.and(Restrictions.eq("name", name), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist Pathology by name successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist Pathology by name, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe la patología", e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existPathologyByInitials(String initials, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist by initials Pathology");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Pathology.class).
                    add((id == null) ? Restrictions.eq("initials", initials)
                    : Restrictions.and(Restrictions.eq("initials", initials), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist by initials Pathology successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist Pathology by initials, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe la patología", e.fillInStackTrace());
        }
    }

    public Pathology getPathologyByName(String name) throws ExceptionDAO {
        try {
            this.logger.logDebug("get Pathology by name");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Pathology.class).
                    add(Restrictions.eq("name", name)).setMaxResults(1);
            return (Pathology) criteria.uniqueResult();
        } catch (HibernateException e) {
            this.logger.logError("error on get Pathology by name, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la patología", e.fillInStackTrace());
        }
    }

    public Pathology getPathologyByInitials(String initials) throws ExceptionDAO {
        try {
            this.logger.logDebug("get Pathology by initials");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Pathology.class).
                    add(Restrictions.eq("initials", initials)).setMaxResults(1);
            return (Pathology) criteria.uniqueResult();
        } catch (HibernateException e) {
            this.logger.logError("error on get Pathology by initials, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la patología", e.fillInStackTrace());
        }
    }
}
